import 'dart:ui' as ui show TextLeadingDistribution;
import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';

import '../../resources/kq_theme_colors.dart';

class KqSelectButton extends StatelessWidget {
  /// 垂直边距，默认8
  final double? verticalPadding;

  /// 水平边距，默认8
  final double? horizontalPadding;

  ///文本超出后样式,默认为超出省略末尾
  final TextOverflow? overflow;

  ///高度
  final double? height;

  ///宽度
  final double? width;

  ///最大行高
  final int maxLines;

  ///按钮文案
  final String text;

  ///文案的间距
  final double? styleHeight;

  ///针对文案的间距 设置居中方式
  final ui.TextLeadingDistribution? leadingDistribution;

  ///按钮对齐文本
  final Alignment? alignment;

  ///文案大小默认24
  final double? font;

  ///背景色
  final Color? backgroundColor;

  ///文案颜色
  final Color? textColor;

  ///边框颜色
  final Color? borderColor;

  ///选中背景色,默认为f4f4f4
  final Color? selectBackgroundColor;

  ///选中边框颜色
  final Color? selectBorderColor;

  ///选中文案颜色
  final Color? selectTextColor;

  ///是否可点默认为true
  final bool isEnable;

  ///选中状态
  final bool isSelect;

  ///弧度，默认为0
  final double? radius;

  /// 点击回调
  final VoidCallback? onTap;

  const KqSelectButton({
    Key? key,
    this.verticalPadding,
    this.horizontalPadding,
    this.radius,
    this.overflow = TextOverflow.ellipsis,
    this.maxLines = 1,
    required this.text,
    this.styleHeight,
    this.leadingDistribution,
    this.font,
    this.height,
    this.width,
    this.alignment = Alignment.center,
    this.backgroundColor = KqThemeColors.bgWhite,
    this.textColor = KqThemeColors.textBlack85,
    this.borderColor = KqThemeColors.bgTransparent,
    this.isSelect = false,
    this.selectBackgroundColor = KqThemeColors.bgF4,
    this.selectTextColor,
    this.selectBorderColor,
    this.isEnable = true,
    this.onTap,
  }) : super(key: key);

  Color? getBackgroundColor(bool select) {
    if (select) {
      if (selectBackgroundColor == null) {
        return backgroundColor;
      } else {
        return selectBackgroundColor;
      }
    } else {
      return backgroundColor;
    }
  }

  Color? getTextColor(bool select) {
    if (select) {
      if (selectTextColor == null) {
        return textColor;
      } else {
        return selectTextColor;
      }
    } else {
      return textColor;
    }
  }

  getBorderColor(bool select) {
    if (select) {
      if (selectBorderColor == null) {
        return borderColor;
      } else {
        return selectBorderColor;
      }
    } else {
      return borderColor;
    }
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: isEnable ? onTap : () {},
      child: Container(
        padding: EdgeInsets.symmetric(
            vertical: verticalPadding ?? 8.r,
            horizontal: horizontalPadding ?? 8.r),
        height: height,
        width: width,
        alignment: alignment,
        constraints: (width != null || height != null)
            ? BoxConstraints(
                maxHeight: height != null
                    ? (height! - 2 * (verticalPadding ?? 8.r))
                    : double.infinity,
                maxWidth: width != null
                    ? ((width! - 2 * (horizontalPadding ?? 8.r)))
                    : double.infinity)
            : null,
        decoration: BoxDecoration(
          color: getBackgroundColor(isSelect),
          borderRadius: BorderRadius.all(Radius.circular(radius ?? 0)),
          border: Border.all(width: 1, color: getBorderColor(isSelect)),
        ),
        child: Text(
          text,
          maxLines: maxLines,
          overflow: overflow,
          style: TextStyle(
              color: getTextColor(isSelect),
              fontSize: font ?? 24.sp,
              height: styleHeight,
              leadingDistribution: leadingDistribution),
        ),
      ),
    );
  }
}
